@*
    Main shape for Admin display types of MenuItem stereotype items, which is used to render
    a menu item in the hierarchy editor.
    It is recursive so that if the menu item has MenuItemsListPart the sub items will also
    be rendered.
*@

@using Newtonsoft.Json;
@using Newtonsoft.Json.Linq;

@inject OrchardCore.ContentManagement.Display.IContentItemDisplayManager ContentItemDisplayManager
@inject OrchardCore.DisplayManagement.ModelBinding.IUpdateModelAccessor ModelUpdaterAccessor

@{
    ContentItem contentItem = Model.ContentItem;
    JArray termsArray = contentItem.Content.Terms;
    var index = 0;
}

<li class="menu-item" id="menu-id-@Model.Index" data-index="@Model.Index">
    <div class="menu-item menu-item-title list-group-item">
        <div class="properties">
            <div class="related">
                <a asp-action="Edit"
                   asp-controller="Admin"
                   asp-route-area="OrchardCore.Taxonomies"
                   asp-route-taxonomyContentItemId="@Model.TaxonomyPart.ContentItem.ContentItemId"
                   asp-route-taxonomyItemId="@Model.ContentItem.ContentItemId"
                   class="btn btn-success btn-sm">
                    @T["Edit"]
                </a>
                <a class="btn btn-primary btn-sm" 
                    asp-route-action="Create" 
                    asp-route-controller="Admin" 
                    asp-route-id="@Model.TaxonomyPart.TermContentType" 
                    asp-route-taxonomyContentItemId="@Model.TaxonomyPart.ContentItem.ContentItemId" 
                    asp-route-taxonomyItemId="@Model.ContentItem.ContentItemId"
                    asp-route-area="OrchardCore.Taxonomies">
                    @T["Add"]
                </a>
                <a asp-action="Delete"
                   asp-controller="Admin"
                   asp-route-area="OrchardCore.Taxonomies"
                   asp-route-taxonomyContentItemId="@Model.TaxonomyPart.ContentItem.ContentItemId"
                   asp-route-taxonomyItemId="@Model.ContentItem.ContentItemId"
                   itemprop="RemoveUrl UnsafeUrl"
                   class="btn btn-danger btn-sm">
                    @T["Delete"]
                </a>
            </div>
            
            <span>@contentItem</span>
            
            @if (Model.Content != null)
            {
                @await DisplayAsync(Model.Content)
            }
        </div>
    </div>

    <ol class="menu-item menu-item-links">
        @if (termsArray != null)
        {
            var updater = ModelUpdaterAccessor.ModelUpdater;

            @foreach (JObject termObject in termsArray)
            {
                var term = termObject.ToObject<ContentItem>();
                dynamic termShape = await ContentItemDisplayManager.BuildDisplayAsync(term, updater, "TermAdmin");
                termShape.TaxonomyPart = Model.TaxonomyPart;
                termShape.Index = Model.Index + "-" + index++;
                @await DisplayAsync(termShape)
            }
        }
    </ol>
</li>